package com.example.mybatisplusdemo.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplusdemo.mapper.OrderMainMapper;
import com.example.mybatisplusdemo.model.domain.OrderSubmitRequest;
import com.example.mybatisplusdemo.model.dto.OrderDTO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.service.IOrderMainService;
import com.example.mybatisplusdemo.model.domain.OrderMain;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lqj
 * @since 2025-07-02
 * @version v1.0
 */
@RestController
@RequestMapping("/api/orderMain")
public class OrderMainController {

    private final Logger logger = LoggerFactory.getLogger( OrderMainController.class );

    @Autowired
    private IOrderMainService orderMainService;
    @Autowired
    private OrderMainMapper orderMainMapper;


    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse<OrderMain> getById(@PathVariable("id") Long id)throws Exception {
        OrderMain orderMain = orderMainService.getById(id);
        return JsonResponse.success(orderMain);
    }

    @PostMapping("/submit")
    public JsonResponse submitOrder(@RequestBody OrderSubmitRequest request) {
        return orderMainService.submitOrder(request);
    }


    @GetMapping("/listByUser/{userId}")
    public JsonResponse<List<OrderMain>> listOrdersByUser(@PathVariable Long userId) {
        List<OrderMain> orders = orderMainService.getOrdersByUserId(userId);
        return JsonResponse.success(orders);
    }

    @PutMapping("/cancel/{orderId}")
    public JsonResponse<Boolean> cancelOrder(@PathVariable Long orderId) {
        int updated = orderMainService.cancelOrder(orderId);
        return updated > 0 ? JsonResponse.success(true) : JsonResponse.failure("订单不存在或取消失败");
    }

    @GetMapping("/order")
    public JsonResponse<List<OrderDTO>> getOrderList() {
        return orderMainService.getAllOrders();
    }

    @PostMapping("/orders/deliver/{id}")
    public JsonResponse<Boolean> deliver(@PathVariable("id") Long orderId) {
        boolean success = orderMainService.deliverOrder(orderId);
        return success ? JsonResponse.success(true) : JsonResponse.failure("发货失败");
    }

    @PostMapping("/orders/confirm/{orderId}")
    public JsonResponse confirmReceive(@PathVariable Long orderId) {
        boolean result = orderMainService.confirmReceive(orderId);
        return result ? JsonResponse.success(true) : JsonResponse.failure("订单状态不允许确认收货");
    }

    @PostMapping("/applyRefund")
    public JsonResponse<Boolean> applyRefund(@RequestParam Long orderId) {
        return orderMainService.applyRefund(orderId);
    }

    @GetMapping("/refundList")
    public JsonResponse<List<OrderMain>> listRefundOrders() {
        List<OrderMain> list = orderMainMapper.selectList(
                new QueryWrapper<OrderMain>().eq("status", 4)
        );
        return JsonResponse.success(list);
    }

    @PostMapping("/approveRefund")
    public JsonResponse<Boolean> approveRefund(@RequestParam Long orderId) {
        return orderMainService.approveRefund(orderId);
    }

    @PostMapping("/rejectRefund")
    public JsonResponse<Boolean> rejectRefund(@RequestParam Long orderId) {
        return orderMainService.rejectRefund(orderId);
    }



}

